package com.hiray.taglayoutmanager;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class DetachViewActivity extends AppCompatActivity {


    @BindView(R.id.Parent)
    LLM Parent;
    @BindView(R.id.add_child)
    AppCompatButton addChild;
    @BindView(R.id.detach_child)
    AppCompatButton detachChild;
    @BindView(R.id.attach_child)
    AppCompatButton attachChild;

    TextView child;
    List<View> addedViews = new ArrayList<>();
    List<View> detachedViews = new ArrayList<>();
    int index = 0;
    @BindView(R.id.reLayout)
    AppCompatButton reLayout;
    @BindView(R.id.addVG)
    AppCompatButton addVG;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detach_view);
        ButterKnife.bind(this);
        work();
    }

    private void work() {
        addChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = brewView();
                addedViews.add(view);
                Parent.addView(view);
            }
        });

        detachChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = addedViews.remove(addedViews.size() - 1);
                Parent.detachViewFromParent(view);
                detachedViews.add(view);
            }
        });

        attachChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = detachedViews.remove(detachedViews.size() - 1);
                LayoutParams lp = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
                Parent.attachViewToParent(view, Parent.getChildCount() - 1, lp);

            }
        });
        reLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Parent.requestLayout();
            }
        });

        addVG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.viewgroup, null);
                addedViews.add(view);
                Parent.addView(view);
            }
        });
    }

    private View brewView() {
        AppCompatTextView child = new AppCompatTextView(this);
        child.setText("I am dynamically added Child" + (++index));
        child.setTextSize(25);
        return child;
    }
}
